const Mock = require('mockjs')

const areaList = []
const count = 100

for (let i = 0; i < count; i++) {
  areaList.push(Mock.mock({
    Id: '@increment',
    name: '@name(5, 10)',
  }))
}

const templateList = []

for (let i = 0; i < count; i++) {
  templateList.push(Mock.mock({
    Name: '@name(5, 10)',
    Id: '@increment',
    Description: '@name(5, 10)'
  }))
}


const equipmentLizt = []

for (let i = 0; i < count; i++) {
  equipmentLizt.push({
    Code: '@title(5, 10)',
    Mac: '@increment',
    Ip: '@increment',
    Manufactor: '@name(5, 10)',
    Addr: '@name(5, 10)',
    'DeviceStatus|1': ["在线", "离线"],
    Id: '@increment'
  })
}

const packageList = []

for (let i = 0; i < count; i++) {
  packageList.push(Mock.mock({
    Code: '@title(5, 10)',
    'Version|1-10.1-10': 1,
    'FileSize|1-100': 1,
    Descritpion: '@title(5, 10)',
    Dt: /2020\-([01][0-9])|(2[0-3])\-([0-5][0-9])/,
    Id: '@increment'
  }))
}

module.exports = [
  {
    url: '/Operator/Login',
    type: 'get',
    response: (req, res) => {
      console.log(req.query)
      res.setHeader('user-sign', 'role')
      return {
        Result: true
      }
    }
  },
  {
    url: '/Operator/ResetPassword',
    type: 'post',
    response: (req, res) => {
      return {
        Result: false
      }
    }
  },
  {
    url: '/Area/GetAreas',
    type: 'get',
    response: config => {
      const { importance, type, title, page = 1, limit = 20, sort } = config.query

      let mockList = areaList.filter(item => {
        if (importance && item.importance !== +importance) return false
        if (type && item.type !== type) return false
        if (title && item.title.indexOf(title) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        Data: pageList,
        Total: mockList.length
      }
    }
  },

  {
    url: '/Area/EditArea',
    type: 'post',
    response: config => {
      const { Id, name } = config.body;

      for (const item of areaList) {
        if (Id === item.Id) {
          item.name = name
          break
        }
      }

      return {
        Result: true,
        Msg: 'success'
      }
    }
  },
  {
    url: '/Area/DelArea',
    type: 'post',
    response: config => {
      const { Id } = config.body
      const findIndex = areaList.findIndex(item => item.Id === Id)
      areaList.splice(findIndex, 1)
      return {
        Result: true
      }
    }
  },
  {
    url: '/ConfigTemplate/GetConfigTemplate',
    type: 'get',
    response: request => {
      const { name, limit, page } = request.query
      const pageList = templateList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        Result: true,
        Data: pageList,
        Total: templateList.length
      }
    }
  },
  {
    url: '/ConfigTemplate/EditConfigTemplate',
    type: 'post',
    response: request => {
      const { Id, Name, Description } = request.body
      for (const item of areaList) {
        if (Id === item.Id) {
          item.Name = Name
          item.Description = Description
          break
        }
      }

      return {
        Result: true
      }
    }
  },
  {
    url: '/Device/GetDevices',
    type: 'get',
    response: request => {
      const { code, limit, page } = request.query
      const pageList = equipmentLizt.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        Result: true,
        Data: pageList,
        Total: equipmentLizt.length
      }
    }
  },
  {
    url: '/Area/GetAreaAll',
    type: 'get',
    response: _ => {
      return {
        Result: true,
        Data: areaList,
        Total: areaList.length
      }
    }
  },

  {
    url: '/App/GetPackages',
    type: 'get',
    response: request => {
      const { limit, page } = request.query
      const pageList = packageList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        Result: true,
        Data: pageList,
        Total: packageList.length
      }
    }
  },

  {
    url: '/App/GetAppHistoryFromDevice',
    type: 'get',
    response: (request) => {
      const { limit, page } = request.query
      const pageList = packageList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        Result: true,
        Data: pageList,
        Total: packageList.length,
        E: 3.14
      }
    }
  },

  {
    url: '/Dict/GetPackageVersions',
    type: 'get',
    response: _ => {
      const pageList = packageList.map(item => ({ Id: item.Id, Version: item.Version }))
      return {
        Result: true,
        Data: pageList,
      }
    }
  },
  {
    url: '/App/GetPackageVersions',
    type: 'get',
    response: _ => {
      const pageList = packageList.map(item => ({ Id: item.Id, Version: item.Version }))
      return {
        Result: true,
        Data: pageList,
      }
    }
  },
  {
    url: '/Device/GetDevicePrototype',
    type: 'get',
    response: request => {
      const { code, limit, page } = request.query
      const pageList = equipmentLizt.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        Result: true,
        Data: pageList,
        Total: equipmentLizt.length
      }

    }
  }
]
